package com.houtai.service.job;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.Page;
import com.houtai.base.ResultVo;
import com.houtai.bean.*;
import com.houtai.bean.dto.job.*;
import com.houtai.bean.vo.job.*;
import com.houtai.bean.vo.job.search.TdJobSearchList;
import com.houtai.bean.vo.job.search.TdJobSearchListVo;
import com.houtai.dao.TdCompanyDao;
import com.houtai.dao.TdJobDao;
import com.houtai.dao.TdJobSearchDao;
import com.houtai.dao.TsJzDictDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JobService {
    private static Logger logger = Logger.getLogger(JobService.class);
    @Autowired
    private TdJobDao tdJobDao;
    @Autowired
    private TsJzDictDao tsJzDictDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TdCompanyDao tdCompanyDao;
    @Autowired
    private TdJobSearchDao tdJobSearchDao;
    /**
     * @Purpose:    添加岗位
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo insertJob(TdJobInsertDto dto, String token) {
        String logInfo = this.getClass().getName() + ":insertJob:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {

            TsJzDict diploma = tsJzDictDao.findDictById(dto.getDiploma_id());
            if (diploma != null && diploma.getCode_type().equals("diploma")) {
                TsJzDict wage = tsJzDictDao.findDictById(dto.getWage_id());
                if (wage != null && wage.getCode_type().equals("wage")) {
                    String[] split = dto.getJob_keywords().split(",");
                    if (split.length > 5) {
                        retInfo.setMark("1");
                        retInfo.setTip("搜索关键词最多5个");
                    } else {
                        User user = (User)redisTemplate.opsForValue().get(token);
                        TdCompany tdCompany = tdCompanyDao.findTdCompanyByUserId(user.getUser_id());
                        TdJob tdJob = new TdJob();
                        tdJob.setCompany_id(tdCompany.getCompany_id());
                        tdJob.setDiploma(diploma.getCode_name());
                        tdJob.setDiploma_id(dto.getDiploma_id());
                        tdJob.setJob_keywords(dto.getJob_keywords());
                        tdJob.setJob_name(dto.getJob_name());
                        tdJob.setJob_specification(dto.getJob_specification());
                        tdJob.setResponsibility(dto.getResponsibility());
                        tdJob.setWage(wage.getCode_name());
                        tdJob.setWage_id(dto.getWage_id());
                        tdJob.setWork_ages(dto.getWork_ages());
                        tdJobDao.insertTdJob(tdJob);
                        retInfo.setMark("0");
                        retInfo.setTip("成功");
                    }

                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("工资区间不存在");
                }
            } else {
                retInfo.setMark("1");
                retInfo.setTip("学历不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    修改岗位
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public ResultVo updateJob(TdJobUpdateDto dto, String token) {
        String logInfo = this.getClass().getName() + ":updateJob:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {

            TsJzDict diploma = tsJzDictDao.findDictById(dto.getDiploma_id());
            if (diploma != null && diploma.getCode_type().equals("diploma")) {
                TsJzDict wage = tsJzDictDao.findDictById(dto.getWage_id());
                if (wage != null && wage.getCode_type().equals("wage")) {
                    User user = (User)redisTemplate.opsForValue().get(token);
                    TdCompany tdCompany = tdCompanyDao.findTdCompanyByUserId(user.getUser_id());
                    TdJob tdJob = tdJobDao.findTdJob(dto.getJob_id());
                    if (tdJob != null && tdJob.getCompany_id() == tdCompany.getCompany_id()) {
                        String[] split = dto.getJob_keywords().split(",");
                        if (split.length > 5) {
                            retInfo.setMark("1");
                            retInfo.setTip("搜索关键词最多5个");
                        } else {
                            tdJob.setDiploma(diploma.getCode_name());
                            tdJob.setDiploma_id(dto.getDiploma_id());
                            tdJob.setJob_keywords(dto.getJob_keywords());
                            tdJob.setJob_name(dto.getJob_name());
                            tdJob.setJob_specification(dto.getJob_specification());
                            tdJob.setResponsibility(dto.getResponsibility());
                            tdJob.setWage(wage.getCode_name());
                            tdJob.setWage_id(dto.getWage_id());
                            tdJob.setWork_ages(dto.getWork_ages());
                            tdJobDao.updateTdJob(tdJob);
                            retInfo.setMark("0");
                            retInfo.setTip("成功");
                        }

                    } else {
                        retInfo.setMark("1");
                        retInfo.setTip("岗位不存在");
                    }

                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("工资区间不存在");
                }
            } else {
                retInfo.setMark("1");
                retInfo.setTip("学历不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    关闭岗位
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public ResultVo updateJobState(TdJobDeleteDto dto, String token) {
        String logInfo = this.getClass().getName() + ":updateJobState:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            TdCompany tdCompany = tdCompanyDao.findTdCompanyByUserId(user.getUser_id());
            TdJob tdJob = tdJobDao.findTdJob(dto.getJob_id());
            if (tdJob != null && tdJob.getCompany_id() == tdCompany.getCompany_id()) {
                tdJobDao.updateTdJobState(tdJob);
                retInfo.setMark("0");
                retInfo.setTip("成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("岗位不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    岗位数据
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public TdJobDetailVo jobDetail(TdJobDeleteDto dto, String token) {
        String logInfo = this.getClass().getName() + ":jobDetail:";
        TdJobDetailVo retInfo = new TdJobDetailVo();
        logger.info("======" + logInfo + "begin======");
        try {

            TdJobDetail tdJob = tdJobDao.findTdJobDetail(dto.getJob_id());
            if (tdJob != null ) {
                retInfo.setDetail(tdJob);
                retInfo.setMark("0");
                retInfo.setTip("成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("岗位不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    我的岗位列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     TdJobListVo
     */
    public TdJobListVo jobList(Page dto, String token) {
        String logInfo = this.getClass().getName() + ":jobList:";
        TdJobListVo retInfo = new TdJobListVo();
        logger.info("======" + logInfo + "begin======");
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            TdCompany tdCompany = tdCompanyDao.findTdCompanyByUserId(user.getUser_id());
            String orderBy = "update_time DESC";
            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), orderBy);
            List<TdJobList> lists = tdJobDao.findTdJobList(tdCompany.getCompany_id());
            PageInfo<TdJobList> list = new PageInfo<>(lists);
            if (list.getTotal() > 0) {

                retInfo.setCurrent_page(dto.getCurrent_page());
                retInfo.setList(list.getList());
                retInfo.setPage_size(list.getPageSize());
                retInfo.setTotal_record(list.getTotal());
                retInfo.setPages(list.getPages());
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("无岗位");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    /**
     * @Purpose:    岗位热门搜索
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public TdJobSearchListVo jobSearchList() {
        String logInfo = this.getClass().getName() + ":jobSearchList:";
        TdJobSearchListVo retInfo = new TdJobSearchListVo();
        logger.info("======" + logInfo + "begin======");
        try {

            List<TdJobSearchList> lists = tdJobSearchDao.findTdJobSearchList();
            retInfo.setJobs(tdJobDao.findHotJobList());
            retInfo.setList(lists);
            retInfo.setMark("0");
            retInfo.setTip("成功");


        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    岗位搜索
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     JobListVo
     */
    public JobListVo jobSearch(JobListDto dto) {
        String logInfo = this.getClass().getName() + ":jobSearch:";
        JobListVo retInfo = new JobListVo();
        logger.info("======" + logInfo + "begin======");
        try {
            TsJzDict city = tsJzDictDao.findDictById(dto.getCity_id());
            if (city != null && city.getCode_type().equals("city")) {
                String orderBy = "update_time DESC";
                PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), orderBy);
                List<JobList> lists = tdJobDao.findJobList(dto);
                PageInfo<JobList> list = new PageInfo<>(lists);
                if (list.getTotal() > 0) {

                    retInfo.setCurrent_page(dto.getCurrent_page());
                    retInfo.setList(list.getList());
                    retInfo.setPage_size(list.getPageSize());
                    retInfo.setTotal_record(list.getTotal());
                    retInfo.setPages(list.getPages());
                    retInfo.setMark("0");
                    retInfo.setTip("成功");
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("无职位");
                }
                TdJobSearch tdJobSearch = tdJobSearchDao.findTdJobSearch(dto.getSearch_param());
                if (tdJobSearch != null) {
                    tdJobSearchDao.updateTdJobSearch(tdJobSearch);
                } else {
                    tdJobSearch = new TdJobSearch();
                    tdJobSearch.setSearch_param(dto.getSearch_param());
                    tdJobSearch.setSearch_size(1);
                    tdJobSearchDao.insertTdJobSearch(tdJobSearch);
                }
            } else {
                retInfo.setMark("1");
                retInfo.setTip("城市不存在");
            }


        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    岗位详情
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     JobListVo
     */
    public JobDetailVo jobDetailVo(JobDetailDto dto) {
        String logInfo = this.getClass().getName() + ":jobDetailVo:";
        JobDetailVo retInfo = new JobDetailVo();
        logger.info("======" + logInfo + "begin======");
        try {
             JobDetail jobDetail = tdJobDao.findJobDetail(dto.getJob_id());
             if (jobDetail != null) {
                 retInfo.setDetail(jobDetail);
                 retInfo.setList(tdJobDao.findCompanyJobList(jobDetail.getCompany_id()));
                 retInfo.setRecommendedJobLists(tdJobDao.findRecommendedJobList(jobDetail.getCompany_id(), jobDetail.getJob_keywords().replaceAll(",", "|")));
                 retInfo.setMark("0");
                 retInfo.setTip("成功");
             } else {
                 retInfo.setMark("1");
                 retInfo.setTip("岗位不存在");
             }


        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
